home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gnulib / libsrc98.zoo / utime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-01  |  3.0 KB  |  150 lines

  1. /* utime -- set the file modification time of the given file
  2.  * according to the time given; a time of 0 means the current
  3.  * time.
  4.  *
  5.  * stime -- set the current time to the value given.
  6.  *
  7.  * All times are in Unix format, i.e. seconds since to
  8.  * midnight, January 1, 1970 GMT
  9.  *
  10.  * written by Eric R. Smith, and placed in the public domain.
  11.  *
  12.  */
  13.  
  14. #include <compiler.h>
  15. #include <limits.h>
  16. #include <time.h>
  17. #include <errno.h>
  18. #include <osbind.h>
  19. #include <mintbind.h>
  20. #ifndef __MINT__
  21. #  define __MINT__
  22. #  include <ioctl.h>
  23. #  undef __MINT__
  24. #else
  25. #  include <ioctl.h>
  26. #endif
  27. #include <assert.h>
  28. #include <unistd.h>
  29. #ifdef __TURBOC__
  30. #include <sys\types.h>
  31. #else
  32. #include <sys/types.h>
  33. #endif
  34. #include "lib.h"
  35.  
  36. extern int __mint;
  37.  
  38. time_t _dostime __PROTO((time_t t));
  39.  
  40. /* convert a Unix time into a DOS time. The longword returned contains
  41.    the time word first, then the date word */
  42.  
  43. time_t
  44. _dostime(t)
  45.     time_t t;
  46. {
  47.         time_t time, date;
  48.     struct tm *ctm;
  49.  
  50.     if ((ctm = localtime(&t)) == NULL)
  51.         return 0;
  52.     time = (ctm->tm_hour << 11) | (ctm->tm_min << 5) | (ctm->tm_sec >> 1);
  53.     date = ((ctm->tm_year - 80) & 0x7f) << 9;
  54.     date |= ((ctm->tm_mon+1) << 5) | (ctm->tm_mday);
  55.     return (time << 16) | date;
  56. }
  57.  
  58. int
  59. utime(_filename, tset)
  60.       const char *_filename;
  61.       const struct utimbuf *tset;
  62. {
  63.     int fh;
  64.     unsigned long actime, modtime;
  65.     unsigned long dtime;    /* dos time equivalent */
  66.     
  67.     char filename[PATH_MAX];
  68.     struct _mutimbuf settime;
  69.     long res;
  70.  
  71.     if (tset)
  72.     {
  73.         modtime = _dostime (tset->modtime);
  74.         actime = _dostime (tset->actime);
  75.     }
  76.     else
  77.     {
  78.         actime = ((long) Tgettime () << 16) | (Tgetdate () & 0xFFFF);
  79.         modtime = actime;
  80.     }
  81.  
  82.     (void)_unx2dos(_filename, filename);
  83.  
  84.     settime.actime = (unsigned short) ((actime >> 16) & 0xFFFF);
  85.     settime.acdate = (unsigned short) (actime & 0xFFFF);
  86.     settime.modtime = (unsigned short) ((modtime >> 16) & 0xFFFF);
  87.     settime.moddate = (unsigned short) (modtime & 0xFFFF);
  88.     res = -EINVAL;
  89.     if (__mint > 92) {
  90.         if (tset)
  91.             res = Dcntl(FUTIME, (long) filename, (long) &settime);
  92.         else
  93.             res = Dcntl(FUTIME, (long) filename, (long) 0);
  94.     }
  95.     if (res != -EINVAL) {
  96.         if (res < 0) {
  97.             errno = (int) -res;
  98.             return -1;
  99.         }
  100.         return 0;
  101.     }
  102.     fh = (int) Fopen(filename, 2);
  103.     if (fh < 0) {
  104. #if 0
  105.         /* Kludge:  return success for dirs even though we failed */
  106.         if ((fh == -ENOENT) && (Fattrib(filename, 0, 0) == FA_DIR))
  107.             return 0;
  108. #endif
  109.         errno = -fh;
  110.         return -1;
  111.     }
  112.  
  113.     if (__mint > 90) {
  114.         if (tset)
  115.             res = Fcntl(fh, (long)&settime, FUTIME);
  116.         else
  117.             res = Fcntl(fh, (long)0, FUTIME);
  118.     }
  119.     if (res == -EINVAL)
  120.           {
  121.         dtime = modtime;
  122.         (void)Fdatime((_DOSTIME *) &dtime, fh, 1);
  123.           }
  124.  
  125.     if ((fh = Fclose(fh)) != 0) {
  126.         errno = -fh;
  127.         return -1;
  128.     }
  129.     return 0;
  130. }
  131.  
  132. int stime(t)
  133.     time_t *t;
  134. {
  135.     unsigned long dtime;
  136.     unsigned date, time;
  137.     long r;
  138.  
  139.     assert(t != 0);
  140.     dtime = _dostime(*t);
  141.     date = (int) (dtime & 0xffff);
  142.     time = (int) (dtime >> 16) & 0xffff;
  143.  
  144.     if (((r = Tsetdate(date)) != 0) || ((r = Tsettime(time)) != 0)) {
  145.         errno = r == -1 ? EBADARG : (int) -r;
  146.         return -1;
  147.     }
  148.     return 0;
  149. }
  150.